#!/usr/bin/perl
use Getopt::Std;
getopt("d:t",\%args);
my $dir = $args{d};
my $td = $args{t}; 
chomp($dir);
if($dir eq "")
{
        die "entered dir name where subfolders are created is empty\n";
}
if($td eq "")
{
        die "entered tempdir name where subfolders are created is empty\n";
}

$geno_file = "processed_beagle_input";
$refsamp = '"CEU|TSI"';
###$dir = '/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_vte/clean/beagle';
###$geno_file = '/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_vte/clean/beagle/processed_beagle_input_vte';
###$refsamp = '"CEU|TSI"';
#$i=21;
	$j = 23;
	system("mkdir $td/$j");
        $tempdir = "$td/$j";
	#print "$geno_file\t$j\t$refsamp\t$tempdir\n";	
	$sys = "sh $dir/bin/runbeagle_ind_X.csh $td/$geno_file $j  $tempdir $dir/bin/plink/plink";
	system($sys);
	$female_ref = "$dir/bin/files/phasedfemale.bgl.gz";
	$male_ref = "$dir/bin/files/phasedmale.bgl.gz";	
	$marker = "$dir/bin/files/markers.txt.gz";
	open(BUFF,"$dir/bin/runbeagle_male_template");
	@array = <BUFF>;
	$line = join("",@array);
	@array_beag_male = split("bbb",$line);
	$line = $array_beag_male[0]."$dir/bin/beagle3.1/bin/beagle.jar".$array_beag_male[1]; 
	@array_z_male = split("zzz",$line);
	open(BUFF,"$dir/bin/runbeagle_female_template");
	@array = <BUFF>;
        $line = join("",@array);
	@array_beag_female = split("bbb",$line);
        $line = $array_beag_female[0]."$dir/bin/beagle3.1/bin/beagle.jar".$array_beag_female[1];

	#print $line."\n";
        @array_z_female = split("zzz",$line);
	$workdir_male = "$tempdir/male/workdir";
	$workdir_female = "$tempdir/female/workdir";
	system("cp $marker $tempdir/male/");
	system("cp $marker  $tempdir/female/");
	system("cp $male_ref $tempdir/male/");
	system("cp $female_ref $tempdir/female/");
	system("gunzip $tempdir/male/markers.txt.gz");
	system("gunzip $tempdir/female/markers.txt.gz");
	system("gunzip $tempdir/male/phasedmale.bgl.gz");
	system("gunzip $tempdir/female/phasedfemale.bgl.gz");
	system("/usr/bin/perl $dir/bin/perl_flip_snp_basedreference_sample.pl -s $tempdir/male/geno_male.bgl  -m $tempdir/male/markers.txt -n $tempdir/male/new_geno_male.bgl");
	system("mv $tempdir/male/new_geno_male.bgl $tempdir/male/geno_male.bgl");
	system("/usr/bin/perl $dir/bin/perl_flip_snp_basedreference_sample.pl -s $tempdir/female/geno_female.bgl -m $tempdir/female/markers.txt -n $tempdir/female/new_geno_female.bgl");
	 system("mv $tempdir/female/new_geno_female.bgl $tempdir/female/geno_female.bgl");

	system("/bin/gzip $tempdir/female/markers.txt");
 	system("/bin/gzip $tempdir/male/markers.txt");
	system("/bin/gzip $tempdir/female/phasedfemale.bgl");
	system("/bin/gzip $tempdir/male/phasedmale.bgl");
	system("/bin/gzip $tempdir/male/geno_male.bgl");
	system("/bin/gzip $tempdir/female/geno_female.bgl");
	$sample_male = "$tempdir/male/geno_male.bgl.gz";
	$sample_female = "$tempdir/female/geno_female.bgl.gz";
	$out_male = "$tempdir/male/beagleout_male";
	$out_female = "$tempdir/female/beagleout_female";
	$line_male = $array_z_male[0]."$tempdir/male/".$array_z_male[1]."$tempdir/male/".$array_z_male[2].$workdir_male.$array_z_male[3].$workdir_male.$array_z_male[4].$workdir_male.$array_z_male[5].$sample_male.$array_z_male[6]."$tempdir/male/markers.txt.gz".$array_z_male[7]."$tempdir/male/phasedmale.bgl.gz".$array_z_male[8].$out_male.$array_z_male[9].$workdir_male.$array_z_male[10]; 
	$line_female = $array_z_female[0]."$tempdir/female/".$array_z_female[1]."$tempdir/female/".$array_z_female[2].$workdir_female.$array_z_female[3].$workdir_female.$array_z_female[4].$workdir_female.$array_z_female[5].$sample_female.$array_z_female[6]."$tempdir/female/markers.txt.gz".$array_z_female[7]."$tempdir/female/phasedfemale.bgl.gz".$array_z_female[8].$out_female.$array_z_female[9].$workdir_female.$array_z_female[10];
	open NUM,"gunzip -c $tempdir/male/geno_male.bgl.gz |" or die "no file $tempdir/male/geno_male.bgl.gz exists\n";
	@num_male = <NUM>;
	open NUM,"gunzip -c $tempdir/female/geno_female.bgl.gz |" or die "no file $tempdir/female/geno_female.bgl exists\n";
	@num_female = <NUM>;
	close(NUM);
	open(WRB,">>$td/submitjobs");
	if(@num_male > 1)
	{
		open(WRBUFF,">$tempdir/male/run_beagle_x_male");
		print WRBUFF $line_male;
		$sys1 = "/home/sge6_2/bin/lx24-amd64/qsub $tempdir/male/run_beagle_x_male";
		print WRB $sys1."\n";
	}
	else
	{
		system("rm -rf $tempdir/male/geno_male.bgl.gz");
		system("rm -rf $tempdir/male/geno_male.bgl");
	}
	if(@num_female > 1)
        {
		open(WRBUFF,">$tempdir/female/run_beagle_x_female");
        	print WRBUFF $line_female;
		$sys2 = "/home/sge6_2/bin/lx24-amd64/qsub $tempdir/female/run_beagle_x_female";
		print WRB $sys2."\n";
	}
	else
	{
		 system("rm -rf $tempdir/female/geno_female.bgl.gz");
		 system("rm -rf $tempdir/female/geno_female.bgl");
	}
